package cn.ycc1.boot3export.controller;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ycc
 * @date 2025/10/27
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/test")
    public String test() {
        return "test";
    }

    private static final String TEMPLATE_PATH = "templates/export-template.docx";

    @GetMapping("/export")
    public void exportUserReport(HttpServletResponse response) {
        try {
            // 准备模板数据
            Map<String, Object> data = new HashMap<>();
            data.put("title", "用户报告");
            data.put("name", "张三");
            data.put("age", 18);
            data.put("email", "zhangsan@example.com");
            data.put("exportDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

            List<Map<String, Object>> details = generateTestData();
            data.put("details", details);

            // 加载模板（使用流方式，兼容 Jar 部署）
            InputStream templateStream = getClass().getClassLoader()
                    .getResourceAsStream(TEMPLATE_PATH);

            ConfigureBuilder builder = Configure.builder(); // 开启spring表达式
            builder.useSpringEL();

            XWPFTemplate template = XWPFTemplate.compile(templateStream, builder.build())
                    .render(data);

            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            response.setHeader("Content-Disposition",
                    "attachment; filename=user-report-" + 12 + ".docx");

            // 输出到响应流
            template.write(response.getOutputStream());
            template.close();

        } catch (Exception e) {
            throw new RuntimeException("导出Word失败", e);
        }
    }

    public static List<Map<String, Object>> generateTestData() {
        // 创建 details 列表
        List<Map<String, Object>> details = new ArrayList<>();

        // 第一个部门
        Map<String, Object> detail1 = new HashMap<>();
        detail1.put("orgName", "财务处");

        // 第一个部门的 fills 数据
        List<Map<String, Object>> fills1 = new ArrayList<>();

        Map<String, Object> fill1 = new HashMap<>();
        fill1.put("fillTitle", "月度财务报表");
        fill1.put("chargeUserNameList", "张三,李四,王五");
        fills1.add(fill1);

        Map<String, Object> fill2 = new HashMap<>();
        fill2.put("fillTitle", "预算执行情况");
        fill2.put("chargeUserNameList", "赵六,钱七");
        fills1.add(fill2);

        detail1.put("fills", fills1);
        details.add(detail1);

        // 第二个部门
        Map<String, Object> detail2 = new HashMap<>();
        detail2.put("orgName", "人力资源处");

        // 第二个部门的 fills 数据
        List<Map<String, Object>> fills2 = new ArrayList<>();

        Map<String, Object> fill3 = new HashMap<>();
        fill3.put("fillTitle", "招聘计划");
        fill3.put("chargeUserNameList", "孙八,周九");
        fills2.add(fill3);

        Map<String, Object> fill4 = new HashMap<>();
        fill4.put("fillTitle", "培训安排");
        fill4.put("chargeUserNameList", "吴十,郑十一");
        fills2.add(fill4);

        Map<String, Object> fill5 = new HashMap<>();
        fill5.put("fillTitle", "绩效考核");
        fill5.put("chargeUserNameList", "王十二");
        fills2.add(fill5);

        detail2.put("fills", fills2);
        details.add(detail2);

        // 第三个部门（测试空数据）
        Map<String, Object> detail3 = new HashMap<>();
        detail3.put("orgName", "技术部");
        detail3.put("fills", new ArrayList<>()); // 空列表测试

        details.add(detail3);
        return details;
    }
}
